package com.suning.sawp.service.impl.sms;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.suning.framework.vcsms.client.entity.Result;
import com.suning.framework.vcsms.client.service.CodeService;
import com.suning.sawp.intf.sms.SmsService;

/**
 * 短信验证码接口
 * @author 14062606
 *
 */
@Service
public class SmsServiceImpl implements SmsService{

	/**
     * 字符串初始化容量
     */
    private static final int INIT_CAPACITY_STRING = 20;

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(SmsServiceImpl.class);

    /**
     * 短信服务器的URL
     */
    @Value("#{smsConfig[vcsms_url]}")
    private String vcsmsUrl;

    private Map<String, String> smsSecretKeyMap;
    
    public Map<String, String> getSmsSecretKeyMap() {
        return smsSecretKeyMap;
    }

    public void setSmsSecretKeyMap(Map<String, String> smsSecretKeyMap) {
        this.smsSecretKeyMap = smsSecretKeyMap;
    }

    /**
     * generateSmsCode 生成短信验证码 〈功能详细描述〉
     * 
     * @param mobileNum
     *            手机号码
     * @param requestIp
     *            短信验证码
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Result generateSmsCode(String mobileNum, String requestIp, String sceneId) {
        return generateSmsCode(mobileNum, mobileNum, requestIp, sceneId);
    }

    /**
     * validateSmsCode 验证短信验证码
     * 
     * @param mobileNum
     *            手机号码
     * @param smsCode
     *            短信验证码 "0":验证成功后不删除,null或其他值表示验证成功就删除
     * @return 返回验证结果
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Result validateSmsCode(String mobileNum, String smsCode, String delFlg, String sceneId) {
        LOGGER.info("Ppm ,validateSmsCode begin.");
        String smsSecretKey =  smsSecretKeyMap.get(sceneId);
        Result result = null;
        try {
        	result = CodeService.validateSmsCode(vcsmsUrl, mobileNum, sceneId, smsCode, smsSecretKey, delFlg);
        } catch (Exception e) {
        	LOGGER.error("validateSmsCode find wrong.", e);
        }

        Gson gson = new Gson();
        LOGGER.info("Ppm ,validateSmsCode request params mobileNum and smsCode = {},validateSmsCode result={}",
                new StringBuilder(INIT_CAPACITY_STRING).append(mobileNum).append("&").append(smsCode).toString(),
                gson.toJson(result));
        return result;
    }

    public void setVcsmsUrl(String vcsmsUrl) {
        this.vcsmsUrl = vcsmsUrl;
    }

    @Override
    public Result generateSmsCode(String mobileNum, String account, String requestIp, String sceneId) {
        String smsSecretKey = this.smsSecretKeyMap.get(sceneId);
        Map<String, String> restrictObjMap = new HashMap<String, String>();
        restrictObjMap.put("account", account);

        if (StringUtils.isNotBlank(requestIp)) {
            restrictObjMap.put("requestIp", requestIp);
        }

        LOGGER.info("Ppm ,generateSmsCode begin. mobileNum={}", mobileNum);

        Result result = CodeService.generateSmsCode(vcsmsUrl, mobileNum, sceneId, restrictObjMap,
                new HashMap<String, String>(), smsSecretKey);

        Gson gson = new Gson();
        Object[] arrs = {mobileNum, String.valueOf(restrictObjMap), gson.toJson(result)};
        LOGGER.info("Ppm ,generateSmsCode request params mobile={}, restrictObjMap = {}, generateSmsCode result={}",
                arrs);

        return result;
    }

}
